<?php
!defined("PHOTO_ADMIN") && exit("ACCESS DENIED");
$job = $_REQUEST['job'];
$url = 'index.php?do=news';
if (empty($job)) {
	$job = 'list';
}
if ($job == 'list') {
	$page = intval($_GET['page']) >0 ? intval($_GET['page']) : 1;
	$limit = 20;
	$offset = ($page - 1) *$limit;
	$count = getcount('news',array());
	$query = $db->query("select * from ".tname('news')." order by id desc limit $offset,$limit");
	$list = $db->fetch_all($query);
	$pagestr = multi($count,$limit,$page,$url);
	include atemplate('do_news_list');
} elseif ($job == 'add' || $job == 'edit') {
	
	if (empty($_POST['step'])) {
		$id = intval($_GET['id']);
		if ($job == 'edit' && $id > 0) {
			$query = $db->query("select * from ".tname('news')." where id = $id");
			$info = $db->fetch_array ($query);
			if (empty($info)) {
				adminmessage('对不起，找不到改信息');	
			}
		}
		include atemplate('do_news_add');
	}else {
		$id = intval($_POST['id']);
		$content = trim($_POST['content']);
		$title = trim($_POST['title']);
		$data['title'] = $title;
		$data['content'] = $content;
		if (empty($data['content'])) {
			adminmessage('请填写公告内容',$url);
		}
		if (empty($data['title'])) {
			adminmessage('请填写公告标题',$url);
		}
		$data['posttime'] = time();
		if (!$id) {
			$data['createtime'] = time();
			inserttable('news',$data);
			adminmessage('添加公告成功',$url);	
		}else{
			updatetable('news',$data,array('id'=>$id));
			adminmessage('修改公告成功',$url);
		}
	}
} elseif ($job == 'del') {
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		$db->query('delete from '.tname('news').' where id='.$id);
		if ($db->affected_rows()) {
			adminmessage('删除公告成功',$url);
		}else {
			adminmessage('删除公告失败',$url);
		}
	}
}
?>